<?php

/**
 * This is the model class for table "hy_member".
 *
 * The followings are the available columns in table 'hy_member':
 * @property string $user_id
 * @property string $user_name
 * @property string $user_pws
 * @property string $passalt
 * @property string $passda
 * @property string $user_truename
 * @property integer $user_sex
 * @property string $user_province
 * @property string $user_city
 * @property string $user_tel
 * @property string $user_add
 * @property string $user_email
 * @property string $user_shen
 * @property string $user_regtime
 * @property string $user_zipcode
 * @property string $user_regip
 * @property string $num
 * @property string $user_mobile
 * @property string $user_company
 * @property string $user_intro
 */
class HyMember extends CActiveRecord
{
	
	public $verifyCode;  //验证码
	public $rePws ;      //确认密码
	public $rememberMe=true;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return HyMember the static model class
	 */
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'hy_member';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_sex', 'numerical', 'integerOnly'=>true),
			array('user_name, user_pws , user_truename, user_province, user_city, user_tel, user_add, user_email, user_zipcode, user_regip, user_mobile, user_company', 'length', 'max'=>200),
			array('passalt, passda', 'length', 'max'=>255),
			array('user_shen', 'length', 'max'=>2),
			array('num', 'length', 'max'=>10),
			array('user_regtime, user_intro', 'safe'),
			array('rememberMe', 'boolean'),  //是否记住信息
			
			array('user_pws', 'required','message'=>'{attribute}不能为空!'), //密码是必须的
			array('user_name', 'required','message'=>'{attribute}不能为空!','on'=>'login'), //密码是必须的
			array('verifyCode', 'required','message'=>'{attribute}不能为空!','on'=>'login,reg'), //密码是必须的
			array('user_email', 'required','message'=>'{attribute}不能为空!','on'=>'reg'),  
			array('user_mobile', 'required','message'=>'{attribute}不能为空!','on'=>'mobilereg'),
			array('user_name,user_email,verifyCode', 'required','message'=>'{attribute}不能为空!','on'=>'toreg'),
			
			array('user_pws,rePws', 'required','message'=>'{attribute}不能为空!','on'=>'reg,toreg,mobilereg'), //密码和确实密码是必须的
			array('rePws', 'compare', 'compareAttribute'=>'user_pws', 'message'=>'两次输入的密码不一致!','on'=>'reg,toreg,mobilereg'),
		
			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'message'=>'验证码不正确!','on'=>'reg,toreg,login'), 
			array('user_email', 'email','message'=>'{attribute}不是合法的邮箱!','on'=>'reg,toreg'),

			array('user_name', 'authenticate', 'on'=>'toreg'),   //验证用户名是否注册
			array('user_mobile', 'authenticate2', 'on'=>'mobilereg'), //验证手机是否合法
			
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, user_name, user_pws, passalt, passda, user_truename, user_sex, user_province, user_city, user_tel, user_add, user_email, user_shen, user_regtime, user_zipcode, user_regip, num, user_mobile, user_company, user_intro', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * 检查用户是否已经注册
	 */
	 public function authenticate($attribute,$params)
    {
        $criteria = new CDbCriteria( ) ; 
		$criteria -> condition = ' (user_name  = :username or user_email  = :username) ' ; 
	    $criteria -> params = array(':username' => $this->user_name) ; 
		$member = $this->model()->find($criteria) ; 
		if($member){
		  	 $this->addError('user_name','用户名已经注册！');
		}
    }
    
    public function authenticate2($attribute,$params)
    {
		$mobilephone=trim($this->user_mobile);    
		//手机号码的正则验证    
		if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$mobilephone)){    
		      $this->addError('user_mobile','手机号码格式不对 ！'); 
		} 
    }
    

	//自定义验证规则
	public function passwordStrength($attribute,$params){
		$this->addError($attribute, 'your password is not strong enough!');
	}
	
	
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'user_name' => '用户名',
			'user_pws' => '密码',
			'passalt' => 'Passalt',
			'passda' => 'Passda',
			'user_truename' => 'User Truename',
			'user_sex' => 'User Sex',
			'user_province' => 'User Province',
			'user_city' => 'User City',
			'user_tel' => 'User Tel',
			'user_add' => 'User Add',
			'user_email' => '邮箱',
			'user_shen' => 'User Shen',
			'user_regtime' => 'User Regtime',
			'user_zipcode' => 'User Zipcode',
			'user_regip' => 'User Regip',
			'num' => 'Num',
			'user_mobile' => '手机',
			'user_company' => 'User Company',
			'user_intro' => 'User Intro',
		    'verifyCode' => '验证码',
		    'rePws' => '确认密码',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('user_pws',$this->user_pws,true);
		$criteria->compare('passalt',$this->passalt,true);
		$criteria->compare('passda',$this->passda,true);
		$criteria->compare('user_truename',$this->user_truename,true);
		$criteria->compare('user_sex',$this->user_sex);
		$criteria->compare('user_province',$this->user_province,true);
		$criteria->compare('user_city',$this->user_city,true);
		$criteria->compare('user_tel',$this->user_tel,true);
		$criteria->compare('user_add',$this->user_add,true);
		$criteria->compare('user_email',$this->user_email,true);
		$criteria->compare('user_shen',$this->user_shen,true);
		$criteria->compare('user_regtime',$this->user_regtime,true);
		$criteria->compare('user_zipcode',$this->user_zipcode,true);
		$criteria->compare('user_regip',$this->user_regip,true);
		$criteria->compare('num',$this->num,true);
		$criteria->compare('user_mobile',$this->user_mobile,true);
		$criteria->compare('user_company',$this->user_company,true);
		$criteria->compare('user_intro',$this->user_intro,true);	

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}